package org.fessingapore.library.controllers;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.fessingapore.library.constants.ItemType;
import org.fessingapore.library.constants.Language;
import org.fessingapore.library.models.Category;
import org.fessingapore.library.models.Item;
import org.fessingapore.library.services.CategoryService;
import org.fessingapore.library.services.ItemService;
import org.fessingapore.library.services.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
@RequestMapping("/category")
public class CategoryController {

	private CategoryService categoryService;

	@Autowired
	public void setCategoryService(CategoryService categoryService) {
		this.categoryService = categoryService;
	}

	@RequestMapping(value = "", method = RequestMethod.GET)
	public String searchAll(Model model) {
		List<Category> categoryList = categoryService.findAll();
		model.addAttribute("categoryList", categoryList);
		model.addAttribute("category", new Category());
		return "config/searchCategory";
	}

	@RequestMapping(value = "", method = RequestMethod.POST)
	public String addCategory(Category category, Model model) {
		categoryService.create(category);
		return "redirect:/category";
	}


	@RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
	public String deleteCategory(@PathVariable String id, Model model) {
		
		if (!id.equals("")){
			Category category = categoryService.getById(Integer.parseInt(id));
			categoryService.delete(category);
		}
		return "redirect:/category";
	}

	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public String updateCategory(Category category, Model model) {
		
		categoryService.update(category);
	
		return "redirect:/category";
	}

	
}
